package com.ss.android.article.base.feature.app.browser;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.app.BizApplication;
import com.ss.android.article.browser.R;
import com.ss.android.common.callback.CallbackCenter;
import com.ss.android.common.callback.SSCallback;
import com.ss.android.common.app.AbsApplication;
import com.ss.android.common.app.LifeCycleInvoker;
import com.ss.android.common.app.LifeCycleMonitor;

public class c extends WebViewClient {
    TextView a;
    SSCallback b;
    int c;
    private View d;
    private LifeCycleMonitor e;
    private boolean f;

    public c() {

    }

    private static void a(View arg2) {
        if (arg2 != null) {
            ViewParent v0 = arg2.getParent();
            if ((v0 instanceof ViewGroup)) {
                ((ViewGroup) v0).removeView(arg2);
            }
        }
    }

    final void a() {
        Context v1 = null;
        int v5 = -3;
        if (this.d != null && this.d.getParent() != null && this.a != null) {
            View v0 = this.d;
            Context v0_1 = v0 != null ? v0.getContext() : v1;
            while (true) {
                if (v0_1 == null) {
                    v0_1 = v1;
                    break;
                } else if (!(v0_1 instanceof Activity)) {
                    if ((v0_1 instanceof ContextWrapper)) {
                        v0_1 = ((ContextWrapper) v0_1).getBaseContext();
                        continue;
                    } else {
                        Logger.w("ViewUtils", "find non-ContextWrapper in view: " + v0_1);
                        v0_1 = v1;
                        break;
                    }
                }

                break;
            }

            if (!android.support.design.a.isComponentViewValid(v0_1)) {
                return;
            }

            AbsApplication absApplication = BizApplication.getApplication();
            AppData.getInstance();
            AppData.isNightMode();
            this.d.setBackgroundColor(ContextCompat.getColor(((Context) absApplication), R.color.c));
            this.a.setTextColor(ContextCompat.getColor(((Context) absApplication), R.color.ssxinzi3));
            this.a.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.fo, 0, 0);
            UIUtils.setLayoutMargins(this.a, v5, v5, v5, ((int) ((((float) this.d.getLayoutParams().height)) * 0.57f)));
        }
    }

    public void onPageFinished(WebView webView, String url) {
        this.f = false;
        if (this.d != null && this.c == 0) {
            com.ss.android.article.base.feature.app.browser.c.a(this.d);
            if (this.b != null) {
                CallbackCenter.removeCallback(com.ss.android.j.b.callbackTypeThemeChanged, this.b);
            }
        }
    }

    public void onPageStarted(WebView webView, String url, Bitmap favicon) {
        if (!this.f) {
            this.c = 0;
            this.f = true;
        }
    }

    public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
        if (webView != null && webView.getContext() != null && (errorCode != this.c || this.d == null || this.d.getParent() == null)) {
            if (this.b == null) {
                this.b = new com.ss.android.article.base.feature.app.browser.d(this);
            } else {
                CallbackCenter.removeCallback(com.ss.android.j.b.callbackTypeThemeChanged, this.b);
            }

            CallbackCenter.addCallback(com.ss.android.j.b.callbackTypeThemeChanged, this.b);
            Context v1 = webView.getContext();
            if (this.d == null) {
                this.d = LayoutInflater.from(v1).inflate(com.ss.android.article.browser.R.layout.ju, null, false);
                this.a = this.d.findViewById(com.ss.android.article.browser.R.id.af1);
                this.d.setOnClickListener(new e(this));
                if (((v1 instanceof LifeCycleInvoker)) && this.e == null) {
                    this.e = new f(this);
                    ((LifeCycleInvoker) v1).registerLifeCycleMonitor(this.e);
                }
            } else {
                com.ss.android.article.base.feature.app.browser.c.a(this.d);
            }

            this.c = errorCode;
            webView.addView(this.d, webView.getWidth(), webView.getHeight());
            this.a();
            UIUtils.setVisibility(this.a, 0);
        }
    }

    @TargetApi(value = 21)
    public WebResourceResponse shouldInterceptRequest(WebView arg2, WebResourceRequest arg3) {
        return super.shouldInterceptRequest(arg2, arg3);
    }

    public WebResourceResponse shouldInterceptRequest(WebView arg2, String arg3) {
        return super.shouldInterceptRequest(arg2, arg3);
    }
}

